<?php
/**
 * Created by PhpStorm.
 * User: 64
 * Date: 2017/3/23 0023
 * Time: 14:26
 */
namespace app\web\controller;
use app\src\product\logic\ProductclassifyLogic;
use app\src\user\action\RelationAction;
use app\src\user\logic\UcenterMemberLogic;
use app\src\user\logic\UserGroupLogic;
use app\src\user\logic\MemberLogic;
use app\src\user\logic\UserMemberLogic;
use app\src\user\logic\MemberConfigLogic;
use app\src\wallet\logic\WalletLogic;
use app\src\wallet\logic\WalletOrderLogicV2;
use app\src\wallet\logic\WalletHisLogicV2;
use app\src\wxpay\logic\WxaccountLogic;
use app\pc\helper\PcFunctionHelper;
use app\src\product\logic\ProductimageLogic;
use app\src\shoppingCart\action\ShoppingCartQueryAction;
use app\src\product\logic\ProductLogic;
use app\src\user\logic\UserRelationLogic;
use app\weixin\Logic\WeixinLogic;
use think\Db;
class Income
{
    //收益处理
    public function test(){
        $this->ahead_income('196','1','1','2','8');
    }


    //前级收益和前前级收益
    /*
     *我的uid，给上级的收益数量，给上级的单个收益钱数，给上上级的收益钱数
     */
    public function ahead_income($my_uid,$count,$each_ahead,$each_double_ahead,$pid=''){
        $each_ahead=$each_ahead*100;
        $each_double_ahead=$each_double_ahead*100;
        if(!empty($pid)){
            $classify=(new ProductclassifyLogic())->getInfo(['pid'=>$pid]);
            $each_ahead=$classify['info']['each_ahead'];
            $each_double_ahead=$classify['info']['each_double_ahead'];
        }

        //查询我的前级
        $upper_count=0;
        $me=(new UserMemberLogic())->getInfo(['uid'=>$my_uid]);
        $my_ahead_uid=$me['info']['father_uid'];
        $my_ahead=(new UserMemberLogic())->getInfo(['uid'=>$my_ahead_uid]);
        $my_ahead_type=$my_ahead['info']['type'];
        if($count==267)$count=237;
        if(!empty($my_ahead_uid)&&$my_ahead_type!==1){
            //如果我有前级，进行收益增加操作
            $note='后级消费，获得奖励，增加提现积分';
            $income_ahead=$count*$each_ahead;
            $a=$this->wallet_update($note,$income_ahead,'1','0',$my_ahead_uid,config('datatree.ahead_plus'));
            $upper_count=$income_ahead;
            if(!$a) return(['status'=>false]);
            //查询是否有前前级
            $my_double_ahead_uid=$my_ahead['info']['father_uid'];
            $my_double_ahead=(new UserMemberLogic())->getInfo(['uid'=>$my_double_ahead_uid]);

            if(!empty($my_double_ahead_uid)&&$my_double_ahead['info']['type']!==1&&($me['info']['group_id']!==1)){
                //如果我有前级，进行收益增加操作
                $note='后后级消费，获得奖励，增加提现积分';
                $income_double_ahead=$count*$each_double_ahead;
                $b=$this->wallet_update($note,$income_double_ahead,'1','0',$my_double_ahead_uid,config('datatree.double_ahead_plus'));
                $upper_count=$upper_count+$income_double_ahead;
                if(!$b) return(['status'=>false]);

            }
        }

        return(['status'=>true,'count'=>$upper_count]);
    }



    //type:0库存积分，1提现积分，
    //status：0加积分，1减积分
    public function wallet_update($note,$money,$type,$status,$uid,$data_tree='0'){

        $wallet=(new WalletLogic())->getInfo(['uid'=>$uid]);

        //扣除积分
        $wallet=(new WalletLogic())->getInfo(['uid'=>$uid]);
        $update_time=time();
        if($type==0){
            if($status==0){
                $after_money=$wallet['info']['stock_points']+$money;

                $wallet_enity=['stock_points'=>$after_money,'update_time'=>$update_time];
            }
            if($status==1){
                $after_money=$wallet['info']['stock_points']-$money;
                $wallet_enity=['stock_points'=>$after_money,'update_time'=>$update_time];
            }
        }elseif($type==1){
            if($status==0){
                $after_money=$wallet['info']['cash_points']+$money;
                $wallet_enity=['cash_points'=>$after_money,'update_time'=>$update_time];
            }
            if($status==1){
                $after_money=$wallet['info']['cash_points']-$money;
                $wallet_enity=['cash_points'=>$after_money,'update_time'=>$update_time];
            }
        }

        $miu=(new WalletLogic())->save(['uid'=>$uid],$wallet_enity);
        $order=empty(session('order'))?0:session('order');
        if($type==0) {
            $miu_log_info = [
                'uid'           => $uid,
                'before_points' => $wallet['info']['stock_points'],
                'points_type'   => '0',
                'after_money'   => $after_money,
                'create_time'   => time(),
                'reason'        => $note,
                'dtree_type'    => $data_tree,
                'wallet_type'   => '0',
                'order'=>$order
            ];
            if($status==0) {$miu_log_info['plus']=$money;$miu_log_info['minus']='0';}
            if($status==1) {$miu_log_info['plus']='0';$miu_log_info['minus']=$money;}

        }else{
            $miu_log_info = [
                'uid'           => $uid,
                'before_points' => $wallet['info']['cash_points'],
                'points_type'   => '1',
                'after_money'   => $after_money,
                'create_time'   => time(),
                'reason'        => $note,
                'dtree_type'    =>  $data_tree,
                'wallet_type'   => '0',
                'order'=>$order
            ];
            if($status==0) {$miu_log_info['plus']=$money;$miu_log_info['minus']='0';}
            if($status==1) {$miu_log_info['plus']='0';$miu_log_info['minus']=$money;}
        }

        $miu_log=(new WalletHisLogicV2())->add($miu_log_info,'id');

        if($miu['status']&&$miu_log){
            return true;
        }
    }



}